const express = require('express');
const router = express.Router();
const db = require('../../../../util/dbConfig/dbConfig')

//修改该用户的手机号
// 该段代码可优化，与修改密码有冗余
router.post('/',async(req,res) => {
    let _id = req.session.userInfo._id
    if(!_id){
        return res.send({
            code: 1,
            msg: '请先登录'
        })
    }
    let {phone} = req.body
    //有id
    try{
        //如果手机号有重复
        let sySql = `select PHONENUM from user where ISDELETE=0 and ID=?`
        let sqlArr1 = [_id]
        let result1 = await db.SySqlConnect(sySql,sqlArr1)
        if(result1 === phone){
            return res.send({
                code: 1,
                msg: '手机号无变化'
            })
        }else{
            //如果密码不重复
            let sqlUpdate =  `update user set PHONENUM=? where ISDELETE=0 and ID=?`
            let sqlUpdateArr = [phone,_id]
            let resultCode = await db.SySqlConnect(sqlUpdate,sqlUpdateArr)

            //需要封装，后期封装，与userList中update重复
            if(!resultCode.changedRows){
                return res.send({
                    code: 2,
                    msg: '修改失败,请重新传入新手机号'
                })
            }else if( resultCode.changedRows === 0){
                return res.send({
                    code: 1,
                    msg: '参数重复，请客户端修复'
                })
            }else if( resultCode.changedRows === 1){
                return res.send({
                    code: 0,
                    msg: '修改成功'
                })
            }else{
                return res.send({
                    code: 1,
                    msg: '参数错误，请客户端修复'
                })
            }
        }

    }catch (e) {
        console.log(e);
        return res.send({
            code: 4,
            msg: '服务器错误，请稍后再试'
        })
    }
})
module.exports = router
